/************************************************************************************/
/*  Program name CAPPOIMP_POSITION_CONTROL.sql                                */
/*  Creater:  CHAN BILLY KA KEUNG (PCCW - UniHub)                                   */
/*************   History   *************************                                */
/* 24 MAY 2006				       First created                                    */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPPOIMP_POSITION_CONTROL';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	lv_seq_id		NUMBER;
	
	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_PO_POSITION_CONTROL
	FOR UPDATE;
		
	
BEGIN
	FOR C1 IN Cur_F LOOP
		BEGIN
			
			SELECT PO_CONTROL_GROUPS_S.NEXTVAL
			INTO lv_seq_id
			FROM DUAL;
			
			INSERT INTO PO_POSITION_CONTROLS_ALL
			VALUES
			(
				CAPPOIMP_MAPPING.GetPositionIDMapping(C1.POSITION_ID),
				lv_seq_id       ,
				C1.LAST_UPDATE_DATE       ,
				C1.LAST_UPDATED_BY        ,
				C1.LAST_UPDATE_LOGIN      ,
				C1.CREATION_DATE          ,
				C1.CREATED_BY             ,
				CAPPOIMP_MAPPING.GetJobIDMapping(C1.JOB_ID),
				CAPBRIMP_MAPPING.GetBGMapping(C1.ORGANIZATION_ID),
				C1.START_DATE,
				C1.END_DATE,
				CAPPOIMP_MAPPING.GetControlGroupIDMapping(C1.CONTROL_FUNCTION_ID),
				C1.PROGRAM_APPLICATION_ID ,
				C1.PROGRAM_ID             ,
				C1.PROGRAM_UPDATE_DATE,
				C1.REQUEST_ID             ,
				C1.ATTRIBUTE1              ,
				C1.ATTRIBUTE10             ,
				C1.ATTRIBUTE11             ,
				C1.ATTRIBUTE12             ,
				C1.ATTRIBUTE13             ,
				C1.ATTRIBUTE14             ,
				C1.ATTRIBUTE15             ,
				C1.ATTRIBUTE2              ,
				C1.ATTRIBUTE3              ,
				C1.ATTRIBUTE4              ,
				C1.ATTRIBUTE5              ,
				C1.ATTRIBUTE6              ,
				C1.ATTRIBUTE7              ,
				C1.ATTRIBUTE8              ,
				C1.ATTRIBUTE9              ,
				C1.ATTRIBUTE_CATEGORY      ,
				CAPBRIMP_MAPPING.GetOrgMapping(C1.ORG_ID)
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;
	
	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/
